﻿using BMC.ARSystem;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class GeneratePropertyBase : IGenerateProperty
    {
        public virtual void Create(CodeTypeDeclaration classType, string propertyName, string propertyType, Limit limit, Dictionary<string, object> attributePropertyValues, GeneratePropertyDelegate propertyDelegate, IGeneratePropertyAttribute attributeGenerator)
        {
            uint id = (uint)attributePropertyValues["DatabaseID"];
            string name = (string)attributePropertyValues["DatabaseName"];
            CodeMemberField _field_ID = new CodeMemberField();
            _field_ID.Name = propertyName + "_ID";
            _field_ID.Type = new CodeTypeReference(typeof(uint));
            _field_ID.Attributes = MemberAttributes.Public | MemberAttributes.Const;
            _field_ID.InitExpression = new CodePrimitiveExpression(id);

            CodeMemberField _field_Name = new CodeMemberField();
            _field_Name.Name = propertyName + "_Name";
            _field_Name.Type = new CodeTypeReference(typeof(string));
            _field_Name.Attributes = MemberAttributes.Public | MemberAttributes.Const;
            _field_Name.InitExpression = new CodePrimitiveExpression(name);

            string htmlFormate = System.Configuration.ConfigurationManager.AppSettings["HtmlFormate"];

            GlobalVar.JScolNames.Append("\"" + name + "\",");
            GlobalVar.koModel.AppendLine("self." + propertyName + "=ko.observable(modelJSON." + propertyName + ");");
            GlobalVar.koModelCreate.AppendLine("self." + propertyName + "=ko.observable();");
            GlobalVar.koHtml.AppendLine(string.Format(htmlFormate,name,id,propertyName,id, name, propertyName));

            string sortAndFormateInfo = string.Empty;

            if (typeof(Int32).Name.Equals(propertyType)) sortAndFormateInfo = ", sorttype: \"int\"";
            if (typeof(Double).Name.Equals(propertyType) || (typeof(Decimal).Name.Equals(propertyType))
                || (typeof(BMC.ARSystem.CurrencyValue).Name.Equals(propertyType))) sortAndFormateInfo = ", sorttype: \"number\"";
            if (typeof(String).Name.Equals(propertyType)) sortAndFormateInfo = ", sorttype: \"text\"";
            if (typeof(DateTime).Name.Equals(propertyType) || typeof(BMC.ARSystem.DateValue).Name.Equals(propertyType))
                sortAndFormateInfo = ", sorttype: \"date\", formatter: \"date\"";

            if (id == 1)
            {
                GlobalVar.JS.AppendLine("{index: \"" + id + "\", name: \"" + propertyName + "\", key: true" + sortAndFormateInfo + "},");
                GlobalVar.JSLocal.AppendLine("{index: \"" + propertyName + "\", name: \"" + "\"" + propertyName + "\", key: true" + sortAndFormateInfo + "},");
            }
            else
            {
                GlobalVar.JS.AppendLine("{index: \"" + id + "\", name: \"" + propertyName + "\"" + sortAndFormateInfo + "},");
                GlobalVar.JSLocal.AppendLine("{index: \"" + propertyName + "\", name: \"" + propertyName + "\"" + sortAndFormateInfo + "},");
            }

            classType.Members.Add(_field_ID);
            classType.Members.Add(_field_Name);
        }
    }
}
